//
//  KLMJoinHomeController.swift
//  KLM_GN
//
//  Created by Nic on 2025/3/12.
//

import UIKit

class KLMJoinHomeController: KLMBaseController {

    @IBOutlet weak var iconView: UIImageView!
    @IBOutlet weak var tipLabel: UILabel!
    @IBOutlet weak var containerView: UIView!
    @IBOutlet weak var textField: UITextField!
    @IBOutlet weak var joinBtn: UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = rgba(243, 243, 246, 1)
    }

    override func viewDidLoadConfig() {
        super.viewDidLoadConfig()
        iconView.image = UIImage(named: "Icon_join_home")
        
        tipLabel.configStyle(with: "请联系管理员为您创建邀请码（家庭设置>添加成员）", font: UIFont(safeName: "PingFangSC-Medium", size: 12), titleColor: rgba(38, 38, 38, 1))
        containerView.backgroundColor = .white
        containerView.layer.cornerRadius = 8.0
        containerView.layer.masksToBounds = true
        
        textField.font = UIFont(safeName: "PingFangSC-Medium", size: 16)
        textField.placeholder = "输入邀请码"
        joinBtn.configStyle(image: "Join_home_disable", state: .disabled)
        joinBtn.configStyle(image: "Join_home_enable", state: .normal)
        joinBtn.isEnabled = false
    }
    
    @IBAction func textChanged(_ sender: UITextField) {
        joinBtn.isEnabled = sender.text != nil && !sender.text!.isEmpty
    }
    
    @IBAction func tapJoin(_ sender: UIButton) {
        KLMRequest.joinToHome(invitationCode: textField.text!) { response in
            
            self.navigationController?.popViewController(animated: true)
            NotificationCenter.default.post(name: .joinHomeSuccess, object: nil)
            
        } failure: { error in
            KLMHttpShowError(error)
        }

    }
    
}
